package tmp;

import geo.math.GMath;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Polygon;
import java.util.ArrayList;
import java.util.Random;

import javax.swing.JPanel;

public class DrawPane extends JPanel{
	
	private static final long serialVersionUID = 1L;
	private ArrayList<Triangle> _tlist = new ArrayList<Triangle>();
	private Placement _pl = new Placement();

	public void paint(Graphics g){
		Graphics2D g2d = (Graphics2D) g;
		
		Random rand = new Random();
		
		for(int i = 0; i < _tlist.size(); i++){
			g2d.setColor(new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256)));
			g2d.fillPolygon(_tlist.get(i).getTriangle());
		}
		
		//test(g2d);
		
	}
	
	public void test(Graphics2D g){
		
		int x[] = {0, 100, 0};
		int y[] = {0, 100, 100};
		
		Polygon p = new Polygon(x, y, 3);
		g.drawPolygon(p);
		
		int pole[] = {5, 5};
		Triangle t = new Triangle(x, y, pole);
		
		int xu = 0;
		int yu = 0;
		if (t.isOnBorder(xu, yu)) System.out.println("is on border = true");
		else System.out.println("is on border = false");
		
		if (t.contain(xu, yu)) System.out.println("contains = true");
		else System.out.println("contains = false");
		
		int xt[] = {xu - 1, xu + 1, xu + 1, xu -1};
		int yt[] = {yu - 1, yu - 1, yu + 1, yu +1};
		
		g.fillPolygon(xt, yt, 4);

	}
	
	
	
	public void next(int d){
		_tlist.add(_pl.placeNext(randomTriangle()));
		repaint();
	}
	
	public Triangle randomTriangle(){
		Triangle t;
		Random rand = new Random();
		int r = rand.nextInt(2);
		
		
		if(r == 1){
			t = new Triangle(0, 0, 70, 0, 40, 30, 10, 10);
		}
		else {
			t = new Triangle(0, 30, 60, 0, 60, 30, 10, 10);
		}
		System.out.println(r);
		return t;
	}
	
}
//---------------------------------------------------------------------
class Triangle{
	
	private int _x[] = new int[3];
	private int _y[] = new int[3];
	private int _pole[] = new int[2];
	
	Triangle(int x1, int y1, int x2, int y2, int x3, int y3, int pole_x, int pole_y){
		_x[0] = x1; _x[1] = x2; _x[2] = x3;
		_y[0] = y1; _y[1] = y2; _y[2] = y3;
		_pole[0] = pole_x;
		_pole[1] = pole_y;
	}
	
	Triangle(int x[], int y[], int pole[]){
		_x = x;
		_y = y;
		_pole = pole;
	}
	
	public Polygon getTriangle(){
		return new Polygon(_x, _y, 3);
	}
	
	public void translate(int x, int y){
		for(int i = 0; i < 3; i++){
			_x[i] += x;
			_y[i] += y;
		}
		_pole[0] += x;
		_pole[1] += y;
	}
	
	public boolean contain(int x, int y){
		Polygon p = new Polygon(_x, _y, 3);
		if (p.contains(x, y)) return true;
		return false;
	}
	
	public int getMinX(){
		int min = _x[0];
		for (int i = 0; i < 3; i++){
			if (min > _x[i]) min = _x[i];
		}
		return min;
	}
	
	public int getMinY(){
		int min = _y[0];
		for (int i = 0; i < 3; i++){
			if (min > _y[i]) min = _y[i];
		}
		return min;
	}
	
	public int getMaxX(){
		int min = _x[0];
		for (int i = 0; i < 3; i++){
			if (min < _x[i]) min = _x[i];
		}
		return min;
	}
	
	public int getMaxY(){
		int min = _y[0];
		for (int i = 0; i < 3; i++){
			if (min < _y[i]) min = _y[i];
		}
		return min;
	}
	
	public boolean isOnBorder(int x, int y){
		GMath m = new GMath();
		if (m.isOnLine(x, y, _x[0], _y[0], _x[1], _y[1])) return true;
		if (m.isOnLine(x, y, _x[1], _y[1], _x[2], _y[2])) return true;
		if (m.isOnLine(x, y, _x[2], _y[2], _x[0], _y[0])) return true;
		return false;
	}
	
	public int[] getXs(){
		return _x;
	}
	
	public int[] getYs(){
		return _y;
	}
	
	public int[] getPole(){
		return _pole;
	}
	
	
	public void setToXY(int x, int y) {
		GMath m = new GMath();
		int xl = (int) m.length(_pole[0], _pole[1], x, _pole[1]);
		int yl = (int) m.length(x, y, x, _pole[1]);
		
		if(x < _pole[0]) xl *= -1;
		if(y < _pole[1]) yl *= -1;
		
		translate(xl, yl);
		//_pole_coords[0] = x;
		//_pole_coords[1] = y;
		
		
	}
}
//---------------------------------------------------------------------
class Placement{
	private short _c = 0;
	private GMath m = new GMath();
	private ArrayList<Triangle> _pl = new ArrayList<Triangle>();
	
	public Triangle placeNext(Triangle t){
		
		if (_c == 0){	//первый в начало координат
			t.translate(-t.getMinX(), -t.getMinY());
			_pl.add(t);
			_c++;
		}
		else if (_c == 1){
			int i = 0;
			//for (int i = 0; i < _pl.size(); i++){
				if (i == 0){
					double l = m.length(0, 0 ,_pl.get(i).getXs()[0], _pl.get(i).getYs()[0]);
					int no = 0;
					for(int k = 0; k < 3; k++){
						if (l > m.length(0, 0 ,_pl.get(i).getXs()[k], _pl.get(i).getYs()[k])){
							l = m.length(0, 0 ,_pl.get(i).getXs()[k], _pl.get(i).getYs()[k]);
							no = k;
						}
					}
					double l1 = m.length(0, 0 ,t.getXs()[0], t.getYs()[0]);
					int no1 = 0;
					for(int k = 0; k < 3; k++){
						if (l1 < m.length(0, 0 , t.getXs()[k], t.getYs()[k])){
							l1 = m.length(0, 0 , t.getXs()[k], t.getYs()[k]);
							no1 = k;
						}
					}
					t.translate(_pl.get(i).getXs()[no] - t.getXs()[no1], _pl.get(i).getYs()[no] - t.getYs()[no1]);
					
					int nk;		//next of 1s
					if (no == 2) nk = 0;
					else nk = no + 1;
					
					
					
					System.out.println(m.length(0, 0, 1, 1));
				}
			//}
			
			
		}
		
		return t;
	}
	
}
